using Engine.Graphics;
using SharpDX;
using System.Windows.Forms;

namespace Scenario_Editor.ObjectPlacer
{
    public class PreviewWindow : UserControl
    {
        #region Private Fields

        private Engine.CameraControl cameraControl1;
        private Panel panel;
        private SceneryObject previewMesh;
        private Engine.Engine previewWindow;

        #endregion Private Fields

        #region Public Methods

        /// <summary>
        /// Initializing the control.
        /// </summary>
        /// <param name="owner"></param>
        public void Init(Form owner)
        {
            InitializeComponent();
            this.previewWindow = new Engine.Engine(this.panel, owner.Handle);
            this.cameraControl1.Camera = this.previewWindow.Camera;

            previewMesh = new SceneryObject(this.previewWindow, Vector3.Zero, 0.0f, 1.0f, "TETEE gfd");
            this.previewWindow.Grafic.AddRenderableObjects(previewMesh.Mesh, 10);
        }

        /// <summary>
        /// Updates the model.
        /// </summary>
        /// <param name="shortFilename"></param>
        public void UpdateModel(string shortFilename)
        {
            this.previewWindow.Grafic.RemoveRenderableObject(this.previewMesh.Mesh);
            this.previewMesh.Dispose();
            this.previewMesh = new SceneryObject(this.previewWindow, Vector3.Zero,
                0, 1, shortFilename);
            this.previewWindow.Grafic.AddRenderableObjects(this.previewMesh.Mesh, 10);
        }

        /// <summary>
        /// Updates the screen and render the scene.
        /// </summary>
        public void UpdateRender()
        {
            //if (previewWindow.Mouse.ButtonL)
            //{
            this.cameraControl1.MoveCamera(previewWindow.Keyboard, previewWindow.Mouse);
            this.previewWindow.Render();
            //}
        }

        #endregion Public Methods

        #region Private Methods

        private void InitializeComponent()
        {
            this.panel = new System.Windows.Forms.Panel();
            this.cameraControl1 = new Engine.CameraControl();
            this.SuspendLayout();
            //
            // panel
            //
            this.panel.Location = new System.Drawing.Point(3, 3);
            this.panel.Name = "panel";
            this.panel.Size = new System.Drawing.Size(265, 164);
            this.panel.TabIndex = 0;
            //
            // cameraControl1
            //
            this.cameraControl1.BackColor = System.Drawing.Color.Transparent;
            this.cameraControl1.Camera = null;
            this.cameraControl1.KeyboardMoveActive = true;
            this.cameraControl1.Location = new System.Drawing.Point(67, 170);
            this.cameraControl1.Margin = new System.Windows.Forms.Padding(0);
            this.cameraControl1.Move = false;
            this.cameraControl1.Name = "cameraControl1";
            this.cameraControl1.RotateAroundTarget = false;
            this.cameraControl1.Size = new System.Drawing.Size(202, 44);
            this.cameraControl1.TabIndex = 1;
            this.cameraControl1.Zoom = false;
            //
            // PreviewWindow
            //
            this.BackColor = System.Drawing.SystemColors.ControlDark;
            this.Controls.Add(this.cameraControl1);
            this.Controls.Add(this.panel);
            this.Name = "PreviewWindow";
            this.Size = new System.Drawing.Size(269, 214);
            this.ResumeLayout(false);
        }

        #endregion Private Methods
    }
}